﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Artigo
/// </summary>
public partial class Artigo
{
    public static void createArtigo(Artigo artigo)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        dc.Artigos.InsertOnSubmit(artigo);
        dc.SubmitChanges();
    }
    public static bool existeArtigoById(string idArtigo)
    {
        try
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            return dc.Artigos.Any(p => p.id.Equals(new Guid(idArtigo)));
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static bool changeArtigo(Guid idArtigo, Guid idTrilha, string titulo, string descricao)
    {
        try
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            Artigo a = dc.Artigos.Single(p => p.id.Equals(idArtigo));
            a.titulo = titulo;
            a.descricao = descricao;
            a.idTrilha = idTrilha;
            dc.SubmitChanges();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public static List<Artigo> getArtigoAprovadoByEvento(Guid idEvento)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Artigos.Where(p => p.aprovado.Equals(true) && p.idEvento.Equals(idEvento)).ToList();
    }
    public static List<Avaliador> getAllAvaliador(Guid idArtigo)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.AvaliacaoArtigos.Where(p => p.idArtigo.Equals(idArtigo)).Select(p => p.Avaliador).ToList();
    }
    public static List<Artigo> getArtigoByEvento(Guid idEvento)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Artigos.Where(p => p.idEvento.Equals(idEvento)).ToList();
    }
    public static List<Artigo> getArtigoByIdAvaliador(Guid idAvaliador)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.AvaliacaoArtigos.Where(p => p.idAvaliador.Equals(idAvaliador)).Select(f => f.Artigo).ToList();
    }
    public static Artigo getArtigoById(Guid idArtigo)
    {
        DataClassesDataContext dc = new DataClassesDataContext();
        return dc.Artigos.Single(p => p.id.Equals(idArtigo));
    }
}